package com.han.hotelplat.manager;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.config.TaskManagementConfigUtils;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.base.Pager;
import com.han.hotelplat.comm.utils.StrUtil;
import com.han.hotelplat.model.EmLogModel;
import com.han.hotelplat.model.HotelLogModel;
import com.han.hotelplat.model.TaskLogModel;
import com.han.hotelplat.pojo.log.Logger;
import com.han.hotelplat.pojo.systemuser.Systemuser;
import com.han.hotelplat.service.systemuser.SystemuserService;

@Component
public class LogManager {
	
	@Autowired
	private EntityManage entityManage;
	@Autowired
	private SystemuserService systemuserService;
	/**
	 * 
		 * @description	发布任务
		 * @author  han1088
		 * @param optcode
		 * @param taskid
		 * @param userid
		 * @param ip
		 * @param realname
		 * @param username
		 * @param context
	 */
	public void creatTaskLog(TaskLogModel tlm, Logger logger){
		logger.setTaskid(tlm.getTaskid());
		logger.setContext(JSONObject.toJSONString(tlm));
		logger.setGroupid(tlm.getOptcode()/1000);
		try{
			entityManage.save(logger);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public void creatEmLog(EmLogModel em, Logger logger){
		logger.setContext(JSONObject.toJSONString(em));
		logger.setGroupid(em.getOptcode()/1000);
		entityManage.save(logger);
	}

	public void creatHotelLog(HotelLogModel hlm, Logger logger) {
		logger.setGroupid(hlm.getOptcode()/1000);
		logger.setContext(JSONObject.toJSONString(hlm));
		entityManage.save(logger);
	}
	
	public void creatChangeTaskState(TaskLogModel tm, Logger logger) {
		logger.setContext(JSONObject.toJSONString(tm));
		logger.setTaskid(tm.getTaskid());
		logger.setGroupid(tm.getOptcode()/1000);
		entityManage.save(logger);
	}
	
	public void submitTaskRemark(TaskLogModel tm, Logger logger) {
		logger.setContext(JSONObject.toJSONString(tm));
		logger.setTaskid(tm.getTaskid());
		logger.setGroupid(tm.getOptcode()/1000);
		entityManage.save(logger);
	}

	@SuppressWarnings("unchecked")
	public Pager<Logger> getAllLogInfoByPage(String startDate, String endDate,
			String context, Integer pageNum) {
		String hql = "From Logger where 1=1 ";
		if(!StrUtil.isNullOrEmpty(startDate)){
			hql = hql+" and creattime>='"+startDate+" 00:00:00'";
		}
		if(!StrUtil.isNullOrEmpty(endDate)){
			hql = hql+" and creattime<='"+endDate+" 23:59:59'";
		}
		if(!StrUtil.isNullOrEmpty(context)){
			hql = hql+" and context like'%"+context+"%'";
		}
		
		hql = hql + " order by creattime desc";
		Pager<Logger> data = entityManage.findByHqlForPage(hql, pageNum, (short)20);
		for(Logger log:data.getList()){
			String logcontext=JSONObject.parseObject(log.getContext()).getString("context");
			log.setContext(logcontext);
			Systemuser user = systemuserService.findUserByUserid(log.getUserid());
			if(user == null){
				continue;
			}
			log.setUsername(user.getUsername());
		}
		return data;
	}

	public List<Logger> getTaskLogInfoByTaskId(String taskid) {
		return entityManage.findByProperty("Logger", "taskid", taskid);
	}
	
	
}
